package cn.kgc.web;

import cn.kgc.entity.Faculty;
import cn.kgc.entity.StuAndPro;
import cn.kgc.service.FacultyService;
import cn.kgc.service.StuAndProService;
import cn.kgc.service.impl.FacultyServiceImpl;
import cn.kgc.service.impl.StuAndProServiceImpl;
import cn.kgc.util.Pager;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.List;
@WebServlet(urlPatterns = "/stuandpro")
public class StuAndProServlet extends BaseServlet{
    public String search(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        int currentPage = 1;
        int currentSize = 4;
//        获取页码
        String currentPagestr = request.getParameter("cp");
        //获取每页显示的条数
        String currentSizestr = request.getParameter("ps");
        if(currentPagestr!=null && !"".equals(currentPagestr)){
            currentPage = Integer.parseInt(currentPagestr);
        }
        if(currentSizestr!=null && !"".equals(currentSizestr)){
            currentSize = Integer.parseInt(currentSizestr);
        }
        //获取学号，姓名，专业进行模糊查询
        String stunum = request.getParameter("stunum");
        String name = request.getParameter("name");
        String specialty =request.getParameter("specialty");

        FacultyService facultyService = new FacultyServiceImpl();
        List<Faculty> facultyList = facultyService.findfaculty();
        StuAndProService stuAndProService = new StuAndProServiceImpl();
        List<StuAndPro> stuAndProList = stuAndProService.search(stunum,name,specialty,currentPage,currentSize);


        //获取总页数，设置起始页码和结束页码
        int totalPage = stuAndProService.totalPage(currentSize,stunum,name,specialty);
        int begin = currentPage-2;
        int end = currentPage+2;
        if(currentPage<=2){
            begin = 1;
        }
        if(currentPage>=totalPage-2){
            end = totalPage;
        }

        //获取总的记录数
        int totalCount = stuAndProService.totalCount(stunum,name,specialty);
        //将总的记录数，当前页码，每页显示条数，页码数量放入pager对象中
        Pager pager = new Pager(totalCount,currentPage,currentSize,10);
        //将查询结果集teacherList放入Pager对象中
        pager.setList(stuAndProList);
        HttpSession session = request.getSession();
        session.setAttribute("facultyList",facultyList);
        session.setAttribute("specialty",specialty);
        session.setAttribute("begin",begin);
        session.setAttribute("end",end);
        session.setAttribute("pager",pager);
        session.setAttribute("currentPage",currentPage);
        session.setAttribute("currentSize",currentSize);
        session.setAttribute("totalPage",totalPage);
        session.setAttribute("stunum",stunum);
        session.setAttribute("name",name);
        session.setAttribute("specialty",specialty);


        return request.getContextPath()+"/adm-stuspesco.jsp";
    }
}
